﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using babydealModel;

public partial class admin_Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    //kiem tra dang nhap
    public int kiemTraDangNhap(string ten_dang_nhap, string mat_khau)
    {
        using (babydealEntities context = new babydealEntities())
        {
            nhan_vien obj = new nhan_vien();
            var kq = from s in context.nhan_vien
                     where s.ten_dang_nhap == ten_dang_nhap && s.mat_khau == mat_khau
                     select s;
            if (kq.Count() != 0)
            {
                return kq.First().id;
                
            }
            else
            {
                return 0;
            }
        }
    }
    // nut dang nhap
    protected void btnDangNhap_Click(object sender, EventArgs e)
    {
        string TenDangNhap = tbxTenDangNhap.Text;
        string MatKhau = tbxMatKhau.Text;

        if (kiemTraDangNhap(TenDangNhap, MatKhau) != 0)
        {
            /*
            // tao cookie lan cuoi truy cap
            HttpCookie time = new HttpCookie("LanTruyCapCuoi", DateTime.Now.ToString());
            time.Expires = DateTime.Now.AddDays(1);
            Response.Cookies.Add(time);

            // tao cookie tai khoan dang nhap
            HttpCookie username = new HttpCookie("TenDangNhap", TenDangNhap);
            username.Expires = DateTime.Now.AddDays(1);
            Response.Cookies.Add(username);

            // tao cookie tai khoan dang nhap
            HttpCookie id = new HttpCookie("IDThanhVien", kiemTraDangNhap(TenDangNhap, MatKhau).ToString());
            id.Expires = DateTime.Now.AddDays(1);
            Response.Cookies.Add(id);*/

            string LanTruyCapCuoi = DateTime.Now.ToString();
            Session.Add("LanTruyCapCuoi", LanTruyCapCuoi);

            Session.Add("TenDangNhap", TenDangNhap);

            int id = kiemTraDangNhap(TenDangNhap, MatKhau);
            Session.Add("IDNhanVien", id);

            Response.Redirect("nguoi_dung.aspx");
        }
        else
        {
            lbQLMK.Text = "Tên đăng nhập hay mật khẩu không hợp lệ!";
        }
    }
}